package president;

import java.util.Date;
import java.sql.Timestamp;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;

import president.PresidentHome;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.Event;

public class AddEvent extends WebPage {
	
	
	
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private Model titleModel = new Model();
	private Model dateModel = new Model();
	private Model timeModel = new Model();
	private Model venueModel = new Model();
	private Model detailModel = new Model();
	
	
	
	
	
	public AddEvent() throws IOException, SQLException {
		
		TextField title = new TextField("etitle", titleModel);
		title.setRequired(true);
		
		TextField date = new TextField("edate", dateModel);
		date.setRequired(true);
		
		TextField time = new TextField("etime", timeModel);
		time.setRequired(true);
		
		TextField venue = new TextField("evenue", venueModel);
		venue.setRequired(true);
		
		TextArea details = new TextArea("edetails", detailModel);
		details.setRequired(true);
		
		
		
		
	    
		
		Form form = new Form("addEventForm"){
		
			
		
		public void onSubmit(){
			
			try {
				String title = (String) titleModel.getObject();
				String date = (String) dateModel.getObject();
				String time = (String) timeModel.getObject();
				String venue = (String) venueModel.getObject();
				String details = (String) detailModel.getObject();
				
				String uploader = ((Sessions)getSession()).getDesc();
				
				Date today = new Date();
				Timestamp timestamp = new Timestamp(today.getTime());
				String ts = timestamp.toString();
				
				System.out.println("title: " + title + " date: " + date + " time: " + time + " venue: " + venue + " details: " + details + " uploader: " + uploader);
				
				Event event = new Event(title, date, time, venue, details, uploader, ts);
				sqlMap.insert("Event.insertEvent", event);
			
				ViewEvents page = new ViewEvents();
				setResponsePage(page);
		}
			catch (SQLException e){
				  e.printStackTrace();
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}};
		
		
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("newEvent", AddEvent.class));
		add(new BookmarkablePageLink("home", PresidentHome.class));
		
		form.add(title);
		form.add(date);
		form.add(time);
		form.add(venue);
		form.add(details);
		
		add(new FeedbackPanel("error"));
		
		
		add(form);
		
	}
}
